﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CommonMvc.Common;
using CommonMvc.Filters;
using CommonMvc.Models;
using CommonMvc.Models.Concreate;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using Newtonsoft.Json;
using NLog;

namespace CommonMvc.Controllers
{

    
    public class BaseController : Controller
    {
        protected DataContext _dataBaseContext;
        protected ICookieManager _cookieManager;
        protected IWorkContext _workContext;
        protected ILogger _logger = LogManager.GetLogger("console");
        protected readonly IDistributedCache _distributedCache;

        public BaseController(DataContext context , IWorkContext workContext, IDistributedCache distributedCache) //依赖注入得到实例  
        {
            _workContext = workContext;
            _dataBaseContext = context;
            _cookieManager = workContext.GetCookieManager();
            _distributedCache = distributedCache;          
        }
        //public W
        public User CurrentUser() {
            if (_workContext == null) {
                return null;
            }
            return _workContext.CurrentUser as User;
        }
        public void BaseLogin(User user) {
            _cookieManager.SetCookie(user.UserType.ToString(), JsonConvert.SerializeObject(user));
        }
        public IActionResult JsonSuccessResult(object data,String msg = "成功处理")
        {
            BaseResult baseResult = new BaseResult
            {
                ResultCode = 0,
                Message = msg,
                Data = data
            };
            return Json(baseResult);
        }
        public IActionResult JsonFailResult(object data,String error = "处理时有错误发生")
        {
            BaseResult baseResult = new BaseResult
            {
                ResultCode = 1,
                Message = error,
                Data = data
            };
            return Json(baseResult);
        }
    }
}